<?php

namespace Admin\Controller;
//use Think\Controller;
use Tools\AdminController;
class RoleController extends AdminController{
  function showlist(){
      $info = D('Role')->select();
      $this->assign('info',$info);
        $this->display();
    }
    function distribute($role_id){
        $role = D('Role');
        //收集被分配的信息
        if(!empty($_POST)){
            $auth_id = $_POST['auth_id'];
            $auth_ids = implode(',', $auth_id);
            $auth_info = D('Auth')->where("auth_id in ($auth_ids)")->select();
            foreach ($auth_info as $k => $v){
                if(!empty($v['auth_c'])){
                $auth_ac .= $v['auth_c'].'-'.$v['auth_a'].',';
                }
            }
           $role_auth_ac = rtrim($auth_ac,',');
           $role ->role_id =$role_id;
           $role ->role_auth_ids = $auth_ids;
           $role ->role_auth_ac = $role_auth_ac;
           $z = $role->save();
           if($z){
               $this->redirect('showlist',array(),2,'分配权限成功!');
           }else{
               $this->redirect('distribute',array(),2,'分配权限失败!');
           }
    }else{
        //查询被分配权限的角色信息
        $role_info = $role->find($role_id);
        $info = explode(',',$role_info['role_auth_ids']);
        //获得可供选取分配的权限信息
        $auth_infoA = D('Auth')->where('auth_level=0')->select();
        $auth_infoB = D('Auth')->where('auth_level=1')->select();
       
       $this->assign('info',$info);   //已有的权限打钩
       $this->assign('auth_infoA',$auth_infoA);
       $this->assign('auth_infoB',$auth_infoB);

        $this -> assign('role_info',$role_info);
        $this->display();
    }
    }
}
